package com.shijie.entity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Pool implements Serializable {

    private Long poolId;

    private String title;

    private Difficulty difficulty;

    private String description;

    // 将String[] 转换成(JSON)String
    private String allType;

    // mybatis不能直接转换Path类型
    private String casesPath;

    private String resultsPath;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;


    public enum Difficulty implements Serializable {

        Easy("easy"),

        Medium("medium"),

        Hard("hard");

        private final String value;

        private Difficulty(String value) {
            this.value = value;
        }

        /**
         * 获取枚举类型的字符串值，
         * <pre>@JsonValue：指定序列化时使用的方法，枚举类型序列化为Json的方法</pre>
         *
         * @return String
         */
        @JsonValue
        public String getValue() {
            return this.value;
        }

        /**
         * 获取枚举类型的字符串值，
         * <pre>@JsonValue：指定序列化时使用的方法，枚举类型序列化为Json的方法</pre>
         *
         * @return String
         */
        @JsonCreator
        public static Difficulty ofValue(String value) {
            for (Difficulty diff : Difficulty.values())
                if (diff.value.equalsIgnoreCase(value))
                    return diff;

            throw new IllegalArgumentException("Pool.Difficulty 未知参数-没有该参数枚举: " + value);
        }

    }


    @Getter
    @ToString
    public static class AllType implements Serializable {

        private final Class<?>[] clazzs;

        private final String[] clazzsStr;

        private AllType(String[] allType) {
            clazzsStr = allType;
            this.clazzs = new Class<?>[allType.length];
            try {
                for (int i = 0; i < allType.length; i++)
                    clazzs[i] = clazzCast(allType[i]);

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        public static AllType of(String[] allType) {
            return new AllType(allType);
        }

        public static AllType of(List<String> allType) {
            return new AllType(allType.toArray(String[]::new));
        }

        private Class<?> clazzCast(String clazz) throws ClassNotFoundException {
            if ("int".equals(clazz))
                return int.class;
            if ("Integer".equals(clazz))
                return Integer.class;
            if ("long".equals(clazz))
                return long.class;
            if ("Long".equals(clazz))
                return Long.class;
            if ("double".equals(clazz))
                return double.class;
            if ("Double".equals(clazz))
                return Double.class;
            if ("String".equals(clazz))
                return String.class;
            if ("List".equals(clazz))
                return List.class;

            if ("int[]".equals(clazz))
                return int[].class;
            if ("Integer[]".equals(clazz))
                return Integer[].class;
            if ("long[]".equals(clazz))
                return long[].class;
            if ("Long[]".equals(clazz))
                return Long[].class;
            if ("double[]".equals(clazz))
                return double[].class;
            if ("Double[]".equals(clazz))
                return Double[].class;
            if ("String[]".equals(clazz))
                return String[].class;
            if ("List[]".equals(clazz))
                return List[].class;

            throw new ClassNotFoundException(clazz + "类未找到，转换失败");
        }

    }

}
